package com.edooteach.tencent.entity.corpuser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
/**
 * 
 * @author cuteHU
 *
 */
public class CorpUser {
        
        /** 成员id */
        private String userid;
        
        /** 成员名称 */
        private String name;
        
        /** 手机号码 */
        private String mobile;
        
        /** 部门id列表 */
        private List<Integer> department;
        
        /** 职位信息 */
        private String position;
        
        /** 性别(0表示未定义，1表示男性，2表示女性) */
        private String gender;
        
        /** 邮箱 */
        private String email;
        
        /** 成员头像的mediaid */
        private String avatarMediaid;
        
        /** 启用/禁用成员。1表示启用成员，0表示禁用成员 */
        private Integer enable;
        
        /** 是否为上级 */
        private Integer isLeader;
        
        /** 二维码 */
        private String qrCode;
        
        /** 座机 */
        private String telephone;
        
        /** 部门内的排序值 */
        private List<Integer> order;
        
        /** 别名 */
        private String alias;
        
        /** 自定义信息 */
        private List<CorpUserExtattr> extattrs;
        
        /** 对外职位 */
        private String externalPosition;
        
        /** 成员对外信息 */
        private List<CorpUserExternalAttr> corpUserExternalAttrs;
        
        /** 关注状态: 1=已关注，2=已冻结，4=未关注 */
        private Integer status;
        
        public CorpUser() {
            super();
        }
        public boolean isValid() {
            return StringUtils.isNotEmpty(userid) && StringUtils.isNotEmpty(name) && 
                    (StringUtils.isNotEmpty(mobile)||StringUtils.isNotEmpty(email));
        }
        
        public Map<String, Object> generateParams() {
            
            Map<String, Object> params = new HashMap<String, Object>();
            
            params.put("userid", this.userid);
            params.put("name", this.name);
            params.put("department", this.department);
            
            if(StringUtils.isNotBlank(this.alias)) {
              params.put("alias", this.alias);
            }
            
            if(StringUtils.isNotBlank(this.mobile)) {
              params.put("mobile", this.mobile);
            }
            
            if(StringUtils.isNotBlank(this.position)) {
              params.put("position", this.position);
            }
            
            if(StringUtils.isNotBlank(this.gender)) {
              params.put("gender", this.gender);
            }
            
            if(StringUtils.isNotBlank(this.email)) {
              params.put("email", this.email);
            }
            
            if(this.isLeader != null) {
              params.put("isleader", this.isLeader);
            }
            
            if(this.enable != null) {
              params.put("enable", this.enable);
            }
            
            if(StringUtils.isNotEmpty(this.avatarMediaid)) {
              params.put("avatar_mediaid", this.avatarMediaid);
            }
            
            if(!this.extattrs.isEmpty()) {
              params.put("extattr", this.generateExtattrsParams());
            }
            
            if(StringUtils.isNotEmpty(this.telephone)) {
              params.put("telephone", this.telephone);
            }
            
            if(StringUtils.isNotEmpty(this.externalPosition)) {
              params.put("external_position", this.externalPosition);
            }
            
            if(!this.corpUserExternalAttrs.isEmpty()) {
              params.put("external_profile", this.generateUserExternalAttrParams());
            }
            
            return params;
        }
        

        
        /**
         * @param corpUserRequest
         * @return
         */
        private Map<String, Object> generateExtattrsParams(){
          
            List<Map<String, Object>> extattrs=new ArrayList<Map<String,Object>>();
            Map<String, Object> extattrMap =new HashMap<String,Object>();
            
            for (CorpUserExtattr extattr : this.extattrs) {
                Map<String, Object> item = new HashMap<String, Object>();
                item.put("name", extattr.getName());
                item.put("value", extattr.getValue());
                extattrs.add(item);
            }
            extattrMap.put("attrs", extattrs);
            return extattrMap;
        }
        
        /**
         * @param corpUserRequest
         * @return
         */
        private Map<String, Object> generateUserExternalAttrParams(){
            
            
            List<Map<String, Object>> external_attr=new ArrayList<Map<String,Object>>();
            
            Map<String, Object> external_profile=new HashMap<String,Object>();
            
            for (CorpUserExternalAttr corpUserExternalAttr: this.corpUserExternalAttrs) {
                if (corpUserExternalAttr.getType()==ExternalAttrType.TEXT) {
                    Map<String, Object> item = new HashMap<String, Object>();
                    item.put("type", 0);
                    item.put("name", corpUserExternalAttr.getName());
                    Map<String,Object> text=new HashMap<String,Object>();
                    text.put("value", corpUserExternalAttr.getContant().getValue());
                    item.put(corpUserExternalAttr.getType().getAlias(), text);
                    external_attr.add(item);
                }else if (corpUserExternalAttr.getType()==ExternalAttrType.WEB) {
                    Map<String, Object> item = new HashMap<String, Object>();
                    item.put("type", 1);
                    item.put("name", corpUserExternalAttr.getName());
                    Map<String,Object> web=new HashMap<String,Object>();
                    web.put("url", corpUserExternalAttr.getContant().getUrl());
                    web.put("title", corpUserExternalAttr.getContant().getTitle());
                    item.put(corpUserExternalAttr.getType().getAlias(), web);
                    external_attr.add(item);
                }else {
                    Map<String, Object> item = new HashMap<String, Object>();
                    item.put("type", 2);
                    item.put("name", corpUserExternalAttr.getName());
                    Map<String,Object> miniprogram=new HashMap<String,Object>();
                    miniprogram.put("appid", corpUserExternalAttr.getContant().getAppid());
                    miniprogram.put("pagepath", corpUserExternalAttr.getContant().getPagepath());
                    miniprogram.put("title", corpUserExternalAttr.getContant().getTitle());
                    item.put(corpUserExternalAttr.getType().getAlias(), miniprogram);
                    external_attr.add(item);
                }
            }
            external_profile.put("external_attr", external_attr);
            return external_profile;
        }
        
        /**
         * @return the userid
         */
        public String getUserid() {
            return userid;
        }
        /**
         * @param userid the userid to set
         */
        public void setUserid(String userid) {
            this.userid = userid;
        }
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
        /**
         * @return the mobile
         */
        public String getMobile() {
            return mobile;
        }
        /**
         * @param mobile the mobile to set
         */
        public void setMobile(String mobile) {
            this.mobile = mobile;
        }
        /**
         * @return the department
         */
        public List<Integer> getDepartment() {
            return department;
        }
        /**
         * @param department the department to set
         */
        public void setDepartment(List<Integer> department) {
            this.department = department;
        }
        /**
         * @return the position
         */
        public String getPosition() {
            return position;
        }
        /**
         * @param position the position to set
         */
        public void setPosition(String position) {
            this.position = position;
        }
        /**
         * @return the gender
         */
        public String getGender() {
            return gender;
        }
        /**
         * @param gender the gender to set
         */
        public void setGender(String gender) {
            this.gender = gender;
        }
        /**
         * @return the email
         */
        public String getEmail() {
            return email;
        }
        /**
         * @param email the email to set
         */
        public void setEmail(String email) {
            this.email = email;
        }
        /**
         * @return the avatarMediaid
         */
        public String getAvatarMediaid() {
            return avatarMediaid;
        }
        /**
         * @param avatarMediaid the avatarMediaid to set
         */
        public void setAvatarMediaid(String avatarMediaid) {
            this.avatarMediaid = avatarMediaid;
        }
        /**
         * @return the enable
         */
        public int getEnable() {
            return enable;
        }
        /**
         * @param enable the enable to set
         */
        public void setEnable(int enable) {
            this.enable = enable;
        }
        /**
         * @return the isLeader
         */
        public int getIsLeader() {
            return isLeader;
        }
        /**
         * @param isLeader the isLeader to set
         */
        public void setIsLeader(int isLeader) {
            this.isLeader = isLeader;
        }
        /**
         * @return the qrCode
         */
        public String getQrCode() {
            return qrCode;
        }
        /**
         * @param qrCode the qrCode to set
         */
        public void setQrCode(String qrCode) {
            this.qrCode = qrCode;
        }
        /**
         * @return the telephone
         */
        public String getTelephone() {
            return telephone;
        }
        /**
         * @param telephone the telephone to set
         */
        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }
        /**
         * @return the order
         */
        public List<Integer> getOrder() {
            return order;
        }
        /**
         * @param order the order to set
         */
        public void setOrder(List<Integer> order) {
            this.order = order;
        }
        /**
         * @return the alias
         */
        public String getAlias() {
            return alias;
        }
        /**
         * @param alias the alias to set
         */
        public void setAlias(String alias) {
            this.alias = alias;
        }
        /**
         * @return the extattrs
         */
        public List<CorpUserExtattr> getExtattrs() {
            return extattrs;
        }
        /**
         * @param extattrs the extattrs to set
         */
        public void setExtattrs(List<CorpUserExtattr> extattrs) {
            this.extattrs = extattrs;
        }
        /**
         * @return the externalPosition
         */
        public String getExternalPosition() {
            return externalPosition;
        }
        /**
         * @param externalPosition the externalPosition to set
         */
        public void setExternalPosition(String externalPosition) {
            this.externalPosition = externalPosition;
        }
        /**
         * @return the corpUserExternalAttrs
         */
        public List<CorpUserExternalAttr> getCorpUserExternalAttrs() {
            return corpUserExternalAttrs;
        }
        /**
         * @param corpUserExternalAttrs the corpUserExternalAttrs to set
         */
        public void setCorpUserExternalAttrs(List<CorpUserExternalAttr> corpUserExternalAttrs) {
            this.corpUserExternalAttrs = corpUserExternalAttrs;
        }
        /**
         * @return the status
         */
        public int getStatus() {
            return status;
        }
        /**
         * @param status the status to set
         */
        public void setStatus(int status) {
            this.status = status;
        }

}
